package com.mano.mall.service.mapper;

import com.mano.mall.service.mapper.entity.GatewayHandlerEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 网关handler数据库访问
 */
public interface GatewayHandlerMapper {

	/**
	 * 获取第一个GatewayHandler
	 * 
	 * @return
	 */
	@Select("SELECT  handler_name AS handlerName,handler_id AS handlerid ,prev_handler_id AS prevhandlerid ,next_handler_id AS nexthandlerid  ,is_open AS ISOPEN FROM mano_gateway_handler WHERE is_open ='1' and prev_handler_id is null;")
	public GatewayHandlerEntity getFirstGatewayHandler();

	@Select("SELECT  handler_name AS handlerName,handler_id AS handlerid ,prev_handler_id AS prevhandlerid ,next_handler_id AS nexthandlerid  ,is_open AS ISOPEN FROM mano_gateway_handler WHERE is_open ='1' and handler_id=#{handlerId}")
	public GatewayHandlerEntity getByHandler(String handlerId);

	@Insert("INSERT INTO mano_gateway_handler VALUES(NULL,#{handlerName},#{handlerId},#{isOpen},0,now(),null,#{sequence},now(),null)")
	void insert(GatewayHandlerEntity gatewayHandlerEntity);

	@Delete("DELETE FROM 'mano_gateway_handler' where ID = #{id")
	int delete(Integer id);

	@Update("UPDATE 'mano_gateway_handler' is_open = {isOpen} where id = #{id} ")
	int updateStatus(GatewayHandlerEntity handlerEntity);

	@Update("UPDATE 'mano_gateway_handler' set sequence = #{sequence} where id = #{id}")
	int updateSequence(GatewayHandlerEntity handlerEntity);

	@Select("SELECT ID as id,handler_id as handlerId from mano_gateway_handler where id=#{id}")
	GatewayHandlerEntity getById(Integer id);
	@Select("SELECT ID as id,handler_id as handlerId from mano_gateway_handler where sequence=#{sequence}")
	GatewayHandlerEntity getBySequence(int pre);

	@Select("SELECT ID as id,handler_id as handlerId from mano_gateway_handler where is_open = 1 order by sequence")
	List<GatewayHandlerEntity> getHandlerList();
}
